var isUseMail = true;
var ckMobilePhoneUnieuq = false;
var fix = false;
_snsLoginConn = "";
var patrn = /^(?!\d)[a-z0-9(\_)]{4,20}$/;
var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1; //google chrome
function checkLoginId() {
    var loginId = jQuery("#loginId").val();
    var email = jQuery("#Email").val();

    var text = loginId.replace(/([\u4E00-\u9FA5])/ig, 'mm');
    var rel = /^(?!\d)[a-zA-Z0-9(\_)(\u4E00-\u9FA5)]{4,20}$/;
    if (loginId == "") {
        jQuery("#userIdMsg").attr("class", "reginfo_wrong").html("用户名不能为空！");
        return false;
    } else if (!/^(?!\d)[a-zA-Z0-9(\_)(\u4E00-\u9FA5)]{4,20}$/.test(text)) {
        jQuery("#userIdMsg").attr("class", "reginfo_wrong").html("用户名是由中文、小写英文字母、数字或下划线组成，不能以数字开头，在4-20字符之间");
        return false;
    } else if (text.search(rel) == -1) {
        jQuery("#userIdMsg").attr("class", "reginfo_wrong").html(
            "用户名是由中文、小写英文字母、数字或下划线组成，不能以数字开头，在4-20字符之间"); //用户名是由中文、小写英文字母、数字或下划线组成，不能以数字开头，在4-20字符之间
        return false;
    } else {
        if (isChrome) {
            jQuery("#userIdMsg").attr("class", "reginfo_trcheck").html("请继续！");
            return true;
        } else {
            /*DWRCheckUserService.judgeLoginID(loginId,function(data){
             if(data){
             jQuery("#userIdMsg").attr("class","reginfo_wrong").html("用户名已经存在！");
             return false;
             }else{
             jQuery("#userIdMsg").attr("class","reginfo_trcheck").html("恭喜您，用户名可以使用！");
             return true;
             }
             });*/
            jQuery.post(frontPath + "/Cart/judgeLoginIDForjQuery.do?T=" + Math.random(), {
                loginId: loginId
            }, function(data) {
                if (jQuery.trim(data) == "true") {
                    jQuery("#userIdMsg").attr("class", "reginfo_wrong").html("用户名已经存在！");
                    return false;
                } else {
                    jQuery("#userIdMsg").attr("class", "reginfo_trcheck").html("恭喜您，用户名可以使用！");
                    return true;
                }
            });
        }
        /*jQuery.post(frontPath+"/Member/judgeLoginResult.do",{loginid:loginId,Email:email},function(msg){
         var datas = msg.split(",");
         if(datas[0]){
         jQuery("#userIdMsg").attr("class","reginfo_wrong").html("用户名已经存在！");
         return false;
         }else{
         jQuery("#userIdMsg").attr("class","reginfo_trcheck").html("恭喜您，用户名可以使用！");
         return true;
         }
         });*/
    }
}

function showMsg(divId) {
    jQuery("#" + divId).show();
}
// 检查Email地址
// var checkEmailFn=new Function("data","checkEmailResult(data)");
function checkEmail() {
    var loginId = jQuery("#loginId").val();
    var email = jQuery("#Email").val();
    var re =
        /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$/;
    if (email == "") {
        jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email地址不能为空！");
        return false;
    } else if (email.length > 128) {
        jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email地址字符数不能大于128 ！");
        return false;
    } else if (email.search(re) == -1) {
        jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email不正确，请换另外一个！");
        return false;
    } else {
        if (isChrome) {
            jQuery("#emailMsg").attr("class", "reginfo_trcheck").html("请继续！");
            return true;
        } else {
            /*DWRCheckUserService.judgeEMail(email,function(data){
             if(data){
             jQuery("#emailMsg").attr("class","reginfo_wrong").html("Email已经存在，请换另外一个！");
             return false;
             }else{
             jQuery("#emailMsg").attr("class","reginfo_trcheck").html("恭喜您，Email地址可以使用！");
             return true;
             }
             });*/
            jQuery.post(frontPath + "/Cart/judgeEMailForjQuery.do?T" + Math.random(), {
                email: email
            }, function(data) {
                if (jQuery.trim(data) == "true") {
                    jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email已经存在，请换另外一个！");
                    return false;
                } else {
                    jQuery("#emailMsg").attr("class", "reginfo_trcheck").html("恭喜您，Email地址可以使用！");
                    return true;
                }
            });
        }
        /*      jQuery.post(frontPath+"/Member/judgeLoginResult.do",{loginid:loginId,Email:email},function(msg){
         var datas = msg.split(",");
         if(datas[1]){
         jQuery("#emailMsg").attr("class","reginfo_wrong").html("Email已经存在，请换另外一个！");
         return false;
         }else{
         jQuery("#emailMsg").attr("class","reginfo_trcheck").html("恭喜您，Email地址可以使用！");
         return true;
         }
         });*/
    }
}

//检查Email作为用户名
function checkLoginIdEmail() {
    var email = jQuery("#Email").val();
    var loginId = email;
    var re =
        /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$/;
    if (email == "") {
        jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email地址不能为空！");
        return false;
    } else if (email.length > 128) {
        jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email地址字符数不能大于128 ！");
        return false;
    } else if (email.search(re) == -1) {
        jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email不正确，请换另外一个！");
        return false;
    } else {
        jQuery.post(frontPath + "/Cart/judgeLoginIDForjQuery.do?T=" + Math.random(), {
            loginId: loginId
        }, function(data) {
            if (jQuery.trim(data) == "true") {
                jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email已经存在，请换另外一个！");
                return false;
            } else {
                jQuery.post(frontPath + "/Cart/judgeEMailForjQuery.do?T" + Math.random(), {
                    email: email
                }, function(data) {
                    if (jQuery.trim(data) == "true") {
                        jQuery("#emailMsg").attr("class", "reginfo_wrong").html("Email已经存在，请换另外一个！");
                        return false;
                    } else {
                        jQuery("#emailMsg").attr("class", "reginfo_trcheck").html("恭喜您，Email地址可以使用！");
                        return true;
                    }
                });
            }
        });

    }
}


// 验证密码
function checkPwd1() {
    if (jQuery("#password").val() == "" || jQuery("#password").val().length < 6 || jQuery("#password").val().length >
        16) {
        jQuery("#pwdMsg").attr("class", "reginfo_wrong").html("密码长度必须为6-16个字符，请重新输入！");
        return false;
    } else {
        jQuery("#pwdMsg").attr("class", "reginfo_trcheck").html("密码格式正确，请继续！");
        return true;
    }
}

function checkPwd2() {
    if (jQuery("#password2").val() == "") {
        jQuery("#pwd1Msg").attr("class", "reginfo_wrong").html("请再输入一遍您上面输入的密码！");
        return false;
    } else if (jQuery("#password").val() != jQuery("#password2").val()) {
        jQuery("#pwd1Msg").attr("class", "reginfo_wrong").html("您输入的密码不一样，请重新输入！");
        return false;
    } else {
        jQuery("#pwd1Msg").attr("class", "reginfo_trcheck").html("两次输入的密码相同，请继续！");
        return true;
    }
}

// 检查验证码
function checkVCode() {
    if (jQuery("#validateCode").val() == "" || jQuery("#validateCode").val().length < 4 || jQuery("#validateCode").val()
        .length > 4) {
        jQuery("#codeMsg").attr("class", "reginfo_wrong").html("请输入图片中的四个字母！");
        return false;
    } else {
        jQuery("#codeMsg").attr("class", "reginfo_trcheck").html("请继续！");
        return true;
    }
}


function checkMobilePhone() {
    var phone = jQuery("#MobilePhone").val()
    $mobilePhone = jQuery("#MobilePhone");
    if (jQuery.trim($mobilePhone.val()) != "") {
        if (isNaN($mobilePhone.val())) {
            jQuery("#MobilePhoneMsg").attr("class", "reginfo_wrong").html("请正确输入您的手机号码！");
            return false;
        } else if ($mobilePhone.val().length != 11) {
            jQuery("#MobilePhoneMsg").attr("class", "reginfo_wrong").html("手机号码为11位，请正确输入！");
            return false;
        } else {
            if (isChrome) {
                jQuery("#MobilePhoneMsg").attr("class", "reginfo_trcheck").html("请继续！");
                return true;
            } else {
                /*           jQuery.ajax(frontPath+"/Member/RuserMobilePhone.do?T="+Math.random(),{mobliephone:phone},function(data){
                 if(jQuery.trim(data)== "true" ){
                 jQuery("#MobilePhoneMsg").attr("class","reginfo_wrong").html("手机号码已经存在，请换另外一个！");
                 return false;
                 }else{
                 jQuery("#MobilePhoneMsg").attr("class","reginfo_trcheck").html("请继续！");
                 return true;
                 }
                 });*/
                var data = jQuery.ajax({
                    url: frontPath + "/Member/RuserMobilePhone.do?T=" + Math.random(),
                    data: {
                        mobliephone: phone
                    },
                    async: false
                }).responseText;
                if (jQuery.trim(data) == "true") {
                    jQuery("#MobilePhoneMsg").attr("class", "reginfo_wrong").html("手机号码已经存在，请换另外一个！");
                    return false;
                } else {
                    jQuery("#MobilePhoneMsg").attr("class", "reginfo_trcheck").html("请继续！");
                    return true;
                }

            }
        }
    } else {
        jQuery("#MobilePhoneMsg").attr("class", "tips1").html("<font color='red'>温馨提示</font>：填写手机号码后将收到我们的促销短信、发货通知");
        return true;
    }
}

function validateInput(obj) {
    var btn = document.getElementById("submitBtn");
    if (!(obj.checked)) {
        btn.style.cursor = "not-allowed";
        btn.disabled = true;
    } else {
        btn.removeAttribute("style");
        btn.removeAttribute("disabled");
    }
}

function checkProtocolCheckbox() {
    var protocol = document.getElementById("protocolCheckbox");
    if (protocol.checked == true) {
        return true;
    } else {
        jQuery("#msgDiv").html("请勾选广购书业服务条款！");
        return false;
    }
}
// 提交表单
function submitForm() {
    //if(checkEmail()==false){
    //return false;
    //}
    //if(checkLoginId()==false){
    //return false;
    //}
    if (checkLoginIdEmail() == false) {
        return false;
    }
    if (checkPwd1() == false) {
        return false;
    }
    if (checkPwd2() == false) {
        return false;
    }
    /*if(checkMobilePhone()==false){

        return false;
    }*/
    /*if(checkVCode()==false){
        return false;
    }*/
    if (checkProtocolCheckbox() == false) {
        return false;
    }
    return true;
}

// 返回结果的iframe加onload事件
function resultIfm() {
    var msg = resultIfram.document.body.innerHTML;
    registerResult(msg);
}

// 分析登录返回代码
function registerResult(msg) {
    var aData = msg.split(",");
    if (isChrome) {
        if (aData.length > 1 && aData[0] == 0) {
            var url = aData[1];
            _snsLoginConn = aData[6];
            var returnUrl = aData[7] == "" ? "" : "?returnUrl=" + aData[7];
            if (_snsLoginConn == "yes") {
                jQuery.getJSON(_snsUrl + "emall/setLogin.jsp?frontEnd_userId=" + aData[3] + "&frontEnd_loginId=" +
                    aData[4] + "&frontEnd_time=" + aData[5] + "&isAdmin=no&emallUrl=" + encodeURIComponent(url +
                        returnUrl) + "&encode=" + aData[8] + "&jsoncallback=?",
                    function(data) {});
                window.location = url + returnUrl;
                //window.location.href=_snsUrl+"emall/setLogin.jsp?frontEnd_userId="+aData[3]+"&frontEnd_loginId="+aData[4]+"&frontEnd_time="+aData[5]+"&isAdmin=no&emallUrl="+encodeURIComponent(url+returnUrl)+"&encode="+aData[8];
            } else {
                window.location = url + returnUrl;
            }
        } else {
            if (aData[0] == 1) {
                jQuery("#loginId").focus();
                jQuery("#userIdMsg").attr("class", "reginfo_wrong").html("用户名已经存在，请换一个！");
                vCodeRefresh();
            }
            if (aData[0] == 2) {
                jQuery("#Email").focus();
                jQuery("#emailMsg").attr("class", "reginfo_wrong").html("email已经存在，请换一个！");
                vCodeRefresh();
            }
            if (aData[0] == 3) {
                jQuery("#msgDiv").html("抱歉，系统繁忙，请稍后再试！");
                vCodeRefresh();
            }
            if (aData[0] == 4) {
                jQuery("#codeMsg").attr("class", "reginfo_wrong").html("验证码输入错误！");
                vCodeRefresh();
            }
            if (aData[0] == 5) {
                jQuery("#codeMsg").attr("class", "reginfo_wrong").html("验证码不能为空");
                vCodeRefresh();
            }
            if (aData[0] == 6) {
                jQuery("#codeMsg").attr("class", "reginfo_wrong").html("手机号码已存在");
                vCodeRefresh();
            }
        }
    } else {
        if (aData[0] == "0") {
            var url = aData[1];
            _snsLoginConn = aData[6];
            var returnUrl = aData[7] == "" ? "" : "?returnUrl=" + aData[7];
            if (_snsLoginConn == "yes") {
                jQuery.getJSON(_snsUrl + "emall/setLogin.jsp?frontEnd_userId=" + aData[3] + "&frontEnd_loginId=" +
                    aData[4] + "&frontEnd_time=" + aData[5] + "&isAdmin=no&emallUrl=" + encodeURIComponent(url +
                        returnUrl) + "&encode=" + aData[8] + "&jsoncallback=?",
                    function(data) {});
                window.location = url + returnUrl;
                //window.location.href=_snsUrl+"emall/setLogin.jsp?frontEnd_userId="+aData[3]+"&frontEnd_loginId="+aData[4]+"&frontEnd_time="+aData[5]+"&isAdmin=no&emallUrl="+encodeURIComponent(url+returnUrl)+"&encode="+aData[8];
            } else {
                window.location = url + returnUrl;
            }
        }
        if (aData[0] == "3") {
            jQuery("#msgDiv").html("抱歉，系统繁忙，请稍后再试！");
            vCodeRefresh();
        }
        if (aData[0] == "4") {
            jQuery("#codeMsg").attr("class", "reginfo_wrong").html("验证码输入错误！");
            vCodeRefresh();
        }
        if (aData[0] == "5") {
            jQuery("#msgDiv").html("发送邮件失败！请检查你的邮件地址是否有效。");
            vCodeRefresh();
        }
    }
}
// 刷新验证码
function vCodeRefresh() {
    jQuery("#ValidateCode").attr("src", frontPath + "/ValidateCode?dumy=" + Math.random());
}
